package com.xqcx.libra.config;

import com.alibaba.otter.canal.client.CanalConnector;
import com.alibaba.otter.canal.client.CanalConnectors;
import lombok.Data;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Description:
 * @author: zhengfei
 * @param:
 * @date: 2018-07-26 下午4:12
 */

@Configuration
@Data
public class CanalConnectorConfig implements DisposableBean {

    @Value("${zkServers}")
    private String zkServers;
    @Value("${destination}")
    private String destination;
    @Value("${batchSize}")
    private int batchSize;

    private String canalUsername = "";
    private String canalPassword = "";
    private CanalConnector canalConnector;

    @Bean
    public CanalConnector getCanalConnector() {
        canalConnector = CanalConnectors.newClusterConnector(zkServers, destination, canalUsername, canalPassword);
        canalConnector.connect();
        // 指定filter，格式 {database}.{table}，这里不做过滤，过滤操作留给用户
        canalConnector.subscribe();
        // 回滚寻找上次中断的位置
        canalConnector.rollback();
        return canalConnector;
    }

    public void destroy() throws Exception {
        if (canalConnector != null) {
            canalConnector.disconnect();
        }
    }

}
